package com.zhudi.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhudi.mapper.UserMapper;
import com.zhudi.model.entity.User;
import com.zhudi.utils.SignatureUtils;
import common.zhudi.commen.ErrorCode;
import common.zhudi.expection.BusinessException;
import common.zhudi.service.InnerUserService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@DubboService
public class InnerUserServiceImpl implements InnerUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public String sayHello(String name) {
        return "hello "+name;
    }

    @Override
    public boolean checkAuthorization(String accessKey, String sendTime, String authorization) {
        Map<String,Object> test = new HashMap<>();
        test.put("name","朱蒂");
        test.put("age",1);
        if (accessKey==null||sendTime==null||authorization==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"加密材料不全");
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accessKey",accessKey);
        queryWrapper.select("secretKey");
        User user = userMapper.selectOne(queryWrapper);
        String secretkey = user.getSecretkey();
        String trueAuthrization = SignatureUtils.signByHmacSHA256(accessKey, secretkey, sendTime);
        return StrUtil.equals(trueAuthrization,authorization);
    }
}
